//
//  BKCMFooterView.m
//  bestkeep
//
//  Created by 毛韶谦 on 2017/1/16.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BKCMFooterView.h"
#import "BKSCCommon.h"


@interface BKCMFooterView ()

@property (nonatomic, strong) UILabel *label1; //全选Label
@property (nonatomic, strong) UILabel *label2;  //合计Label
@property (nonatomic, strong) UILabel *label4;  //是否包含运费
@property (nonatomic, strong) UILabel *label5;  //去结算Label
@property (nonatomic, strong) UILabel *label6;  // （
@property (nonatomic, strong) UILabel *label7;  //  ）
@property (nonatomic, strong) UIButton *button1; // 全选按钮
@property (nonatomic, strong) UIButton *big_button1; // 全选按钮-大家伙
@property (nonatomic, strong) UIButton *button2; // 结算
@property (nonatomic, strong) UIButton *moveSaveButton; // 移到收藏夹
@property (nonatomic, strong) UIButton *deleteButton; // 删除

@end

@implementation BKCMFooterView


- (instancetype)initWithFrame:(CGRect)frame
{
    // Jinghe 2016年05月25日09:01:49
    self = [super initWithFrame:frame];
    if (self) {
        
        self.userInteractionEnabled = YES;
        self.backgroundColor = [UIColor whiteColor] ;
        
        self.layer.shadowColor = [UIColor lightGrayColor].CGColor;
        self.layer.shadowOffset = CGSizeMake(0, -1);
        self.layer.shadowOpacity = 0.4f;
        
        /*------------------------------------*/
        
        //圈圈
        _button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button1 setBackgroundImage:[UIImage imageNamed:@"BKSC_select_image"] forState:UIControlStateSelected];
        [_button1 setBackgroundImage:[UIImage imageNamed:@"BKSC_unselect_image"] forState:UIControlStateNormal];
        _button1.layer.cornerRadius = 9.5f;//8---12
        _button1.layer.masksToBounds = YES;
        
        [self addSubview:_button1];
        
        [_button1 addTarget:self action:@selector(allSelectedButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        _big_button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        _big_button1.backgroundColor = [UIColor clearColor];
        [_big_button1 addTarget:self action:@selector(allSelectedButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_big_button1];
        
        
        _label1 = [UILabel new];
        [_label1 setText:BKSCLocalizedString(@"BKSCFootViewSelectAllLabel")];
        [_label1 setFont:[UIFont systemFontOfSize:15.0]];
        [_label1 setTextColor:HexColor(0x404040)];
        [self addSubview:_label1];
        
        _label2 = [UILabel new];
        [_label2 setText:BKSCLocalizedString(@"BKSCFootViewCombined")];
        [_label2 setFont:[UIFont systemFontOfSize:15.0]];//12---14
        //        [_label2 setFont:[UIFont systemFontOfSize:12.0]];
        [_label2 setTextAlignment:NSTextAlignmentNatural];
        [_label2 setTextColor:HexColor(0x404040)];
        [self addSubview:_label2];
        
        _label3 = [UILabel new];
        [_label3 setText:@"￥0.00"];
        [_label3 setFont:[UIFont systemFontOfSize:15.0]];//12---14
        [_label3 setTextColor:BESTKEEP_RED];
        [self addSubview:_label3];
        
        _label4 = [UILabel new];
        [_label4 setText:BKSCLocalizedString(@"BKSCFootViewNoContainCost")];
        [_label4 setTextColor:HexColor(0x404040)];
        _label4.font = [UIFont systemFontOfSize:13];//9---11
        [self addSubview:_label4];
        
        _button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [_button2 setBackgroundColor:HexColor(0x00BA9C)];
        [self addSubview:_button2];
        [_button2 addTarget:self action:@selector(calculateButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        _label5 = [UILabel new];
        [_label5 setText:BKSCLocalizedString(@"BKFootViewSettlement")];
        [_label5 setFont:[UIFont boldSystemFontOfSize:16.0]];//12---14
        [_label5 setTextColor:[UIColor whiteColor]];
        [_button2 addSubview:_label5];
        
        _label6 = [UILabel new];
        [_label6 setText:@"("];
        [_label6 setFont:[UIFont systemFontOfSize:16.0]];//12---14
        [_label6 setTextColor:[UIColor whiteColor]];
        [_button2 addSubview:_label6];
        
        _accountLable = [UILabel new];
        [_accountLable setText:@"0"];
        [_accountLable setFont:[UIFont systemFontOfSize:16.0]];//12---14
        [_accountLable setTextColor:[UIColor whiteColor]];
        [_button2 addSubview:_accountLable];
        
        _label7 = [UILabel new];
        [_label7 setText:@")"];
        [_label7 setFont:[UIFont systemFontOfSize:16.0]];//12---14
        [_label7 setTextColor:[UIColor whiteColor]];
        [_button2 addSubview:_label7];
        //移入收藏夹
        _moveSaveButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        _moveSaveButton.layer.borderWidth = 0.4;
        _moveSaveButton.layer.borderColor = HexColor(0x404040).CGColor;
        
        
        [_moveSaveButton setBackgroundColor:[UIColor whiteColor]];
        [_moveSaveButton setTitle:BKSCLocalizedString(@"BKFootViewMoveFavorites") forState:UIControlStateNormal];
        [_moveSaveButton setTitleColor:HexColor(0x404040) forState:UIControlStateNormal];
        [_moveSaveButton.titleLabel setFont:[UIFont systemFontOfSize:12.0]];
        _moveSaveButton.layer.cornerRadius = 3;
        _moveSaveButton.layer.masksToBounds = YES;
        [self addSubview:_moveSaveButton];
        [_moveSaveButton addTarget:self action:@selector(moveToSaveButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        //删除
        _deleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [_deleteButton.layer setMasksToBounds:YES];
        [_deleteButton.layer setCornerRadius:0.5];
        [_deleteButton setBackgroundColor:HexColor(0xF83030)];
        [_deleteButton setTitle:BKSCLocalizedString(@"BKFootViewDelegate") forState:UIControlStateNormal];
        [_deleteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_deleteButton.titleLabel setFont:[UIFont systemFontOfSize:12.0]];
        _deleteButton.layer.cornerRadius = 3;
        _deleteButton.layer.masksToBounds = YES;
        [self addSubview:_deleteButton];
        [_deleteButton addTarget:self action:@selector(deleteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        self.moveSaveButton.hidden = YES;
        self.deleteButton.hidden = YES;
        /*------------------------------------*/
        
        [_button1 setTranslatesAutoresizingMaskIntoConstraints:NO];
        [_button1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).mas_equalTo(ApplicationBackGauge);
            make.width.equalTo(@(19));//18---24
            make.height.equalTo(@(19));//18--24
            make.centerY.equalTo(self.mas_centerY);
        }];
        
        
        // 大家伙选择器
        [_big_button1 setTranslatesAutoresizingMaskIntoConstraints:NO];
        [_big_button1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).mas_equalTo(0);
            make.top.equalTo(self.mas_top);
            make.width.equalTo(@(60));//18---24
            make.height.equalTo(@(50));//18--24
            
        }];
        // 全选
        [_label1 setTranslatesAutoresizingMaskIntoConstraints:NO];
        [_label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_button1.mas_right).mas_offset(8);
            make.centerY.equalTo(_button1.mas_centerY);
        }];
        // 结算--- _button2
        [_button2 setTranslatesAutoresizingMaskIntoConstraints:NO];
        [_button2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right);
            make.width.equalTo(@(94));
            make.top.equalTo(self.mas_top);
            make.bottom.equalTo(self.mas_bottom);
            
        }];
        
        // 去结算--- _label5
        [_label5 setTranslatesAutoresizingMaskIntoConstraints:NO];
        [_label5 mas_makeConstraints:^(MASConstraintMaker *make) {
            
            
            
            make.left.equalTo(_button2.mas_left).offset(5);
            make.top.equalTo(_button2.mas_top).offset(15);
            make.bottom.equalTo(_button2.mas_bottom).offset(-15);
            
        }];
        
        // ( --- _label6
        [_label6 setTranslatesAutoresizingMaskIntoConstraints:NO];
        [_label6 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_label5.mas_right);
            
            make.top.equalTo(_label5.mas_top);
            make.bottom.equalTo(_label5.mas_bottom);
            
        }];
        
        [_accountLable setTranslatesAutoresizingMaskIntoConstraints:NO];
        [_accountLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_label6.mas_right);
            
            make.top.equalTo(_label5.mas_top);
            make.bottom.equalTo(_label5.mas_bottom);
            
        }];
        
        // ) --- _label7
        [_label7 setTranslatesAutoresizingMaskIntoConstraints:NO];
        [_label7 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_accountLable.mas_right);
            
            make.top.equalTo(_label5.mas_top);
            make.bottom.equalTo(_label5.mas_bottom);
            
        }];
        
        
        // 不含运费
        [_label4 setTranslatesAutoresizingMaskIntoConstraints:NO];
        [_label4 mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(_label1.mas_right).offset(12);
            make.top.equalTo(_label2.mas_bottom).offset(3);//0----3
            make.height.equalTo(@(14));
        }];
        // 合计:
        [_label2 setTranslatesAutoresizingMaskIntoConstraints:NO];
        [_label2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(7);
            make.left.equalTo(_label1.mas_right).offset(12);
            make.height.equalTo(@(18));
        }];
        // ￥0.00
        [_label3 setTranslatesAutoresizingMaskIntoConstraints:NO];
        [_label3 mas_makeConstraints:^(MASConstraintMaker *make) {
            //            make.right.equalTo(_button2.mas_left).mas_offset(-171);
            //            make.top.equalTo(self.mas_top).mas_offset(3);
            make.centerY.equalTo(_label2.mas_centerY);
            make.left.equalTo(_label2.mas_right).offset(10);
        }];
        
        [_moveSaveButton setTranslatesAutoresizingMaskIntoConstraints:NO];
        [_moveSaveButton mas_makeConstraints:^(MASConstraintMaker *make) {
            //            make.left.equalTo(self.mas_left).offset(226);
            make.width.equalTo(@(80));
            make.height.mas_equalTo(30);
            make.centerY.mas_equalTo(self.mas_centerY);
        }];
        
        [_deleteButton setTranslatesAutoresizingMaskIntoConstraints:NO];
        [_deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_moveSaveButton.mas_right).offset(10);
            make.right.equalTo(self.mas_right).offset(-12);
            make.size.mas_equalTo(CGSizeMake(54, 30));
            make.centerY.mas_equalTo(self.mas_centerY);
        }];
    }
    return self;
}
- (void)layOut_UI{
    
    // 去结算--- _label5
    [_label5 setTranslatesAutoresizingMaskIntoConstraints:NO];
    [_label5 mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        
        if (_accountLable.text.length > 2)  {
            
            make.left.equalTo(_button2.mas_left).offset(2);
            
        }else if (_accountLable.text.length == 2){
            make.left.equalTo(_button2.mas_left).offset(7);
        }else if (_accountLable.text.length <= 1) {
            make.left.equalTo(_button2.mas_left).offset(13);
        }
        
        make.top.equalTo(_button2.mas_top).offset(15);
        make.bottom.equalTo(_button2.mas_bottom).offset(-15);
        
    }];
    
    // ( --- _label6
    [_label6 setTranslatesAutoresizingMaskIntoConstraints:NO];
    [_label6 mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_label5.mas_right);
        
        make.top.equalTo(_label5.mas_top);
        make.bottom.equalTo(_label5.mas_bottom);
        
    }];
    
    [_accountLable setTranslatesAutoresizingMaskIntoConstraints:NO];
    [_accountLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_label6.mas_right);
        
        make.top.equalTo(_label5.mas_top);
        make.bottom.equalTo(_label5.mas_bottom);
        
    }];
    
    // ) --- _label7
    [_label7 setTranslatesAutoresizingMaskIntoConstraints:NO];
    [_label7 mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_accountLable.mas_right);
        
        make.top.equalTo(_label5.mas_top);
        make.bottom.equalTo(_label5.mas_bottom);
        
    }];
    
    
}
-(void)allSelectedButtonClicked:(UIButton *)allSelectedButton{
    allSelectedButton.selected = !allSelectedButton.selected;
    if ([self.footerDelegate respondsToSelector:@selector(footerAccountView:allSelectedButtonClicked:)]) {
        [self.footerDelegate footerAccountView:self allSelectedButtonClicked:allSelectedButton];
    }
}
-(void)moveToSaveButtonClicked:(UIButton *)moveToSaveButgton{
    if ([self.footerDelegate respondsToSelector:@selector(footerAccountView:moveToSaveButtonClicked:)]) {
        [self.footerDelegate footerAccountView:self moveToSaveButtonClicked:moveToSaveButgton];
    }
}
-(void)deleteButtonClicked:(UIButton *)deleteButton{
    if ([self.footerDelegate respondsToSelector:@selector(footerAccountView:deleteButtonClicked:)]) {
        [self.footerDelegate footerAccountView:self deleteButtonClicked:deleteButton];
    }
}
-(void)calculateButtonClicked:(UIButton *)calculateButton{
    if ([self.footerDelegate respondsToSelector:@selector(footerAccountView:calculateButtonClicked:)]) {
        [self.footerDelegate footerAccountView:self calculateButtonClicked:calculateButton];
    }
}


//如果有编辑状态 则footview 就设置为编辑状态
-(void)updateFooterAccountView:(BOOL)isEdit{
    self.moveSaveButton.hidden = !isEdit;
    self.deleteButton.hidden = !isEdit;
    self.label2.hidden = isEdit;
    self.label3.hidden = isEdit;
    self.label4.hidden = isEdit;
    self.button2.hidden = isEdit;
}

-(void)updateAllSelectedButtonSelected:(BOOL)isAllSelected{
    self.button1.selected = isAllSelected;
    self.big_button1.selected = self.button1.selected;
}
- (void)setFooterAmountValue{
    
    [self layOut_UI];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
